package com.opensky.qatest.ui.component;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.opensky.qatest.ui.page.thirdparty.FacebookLoginPage;
import com.opensky.qatest.ui.page.thirdparty.PlaxoContactPage;
import com.opensky.qatest.ui.page.thirdparty.TwitterSignInPage;
import com.opensky.qatest.util.selenium.HTML;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * Invite form form on Invite page
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.0
 * 
 */
public class InvitePageForm {

    // ************************ Locators ************************

    private static final String INVITE_FORM = "//section[@class='account invite big-wrapper clearfix']";

    private static final String PERSONAL_LINK = "//section[@class='account invite big-wrapper clearfix']//input[@id='personal-link' and contains(@value, 'http://osky.co/')]";

    private static final String COPY_LINK_BTN = "//section[@class='account invite big-wrapper clearfix']//button[@id='copy-personal-link' and text()='Copy link']";

    private static final String COPIED_BTN = "//section[@class='account invite big-wrapper clearfix']//button[@id='copy-personal-link' and text()='Copied']";

    private static final String SHARE_ON_FACEBOOK = "//section[@class='account invite big-wrapper clearfix']//span[@class='share-on-fb']/a";

    private static final String SHARE_ON_TWITTER = "//section[@class='account invite big-wrapper clearfix']//span[@class='share-on-twitter']/a";

    private static final String EMAILS_TEXTAREA = "//section[@class='account invite big-wrapper clearfix']//textarea[@id='invite_emails']";

    private static final String ADD_FROM_CONTACTS_BTN = "//section[@class='account invite big-wrapper clearfix']//button[text()='+ Add from your contacts']";

    private static final String SEND_NOW_BTN = "//section[@class='account invite big-wrapper clearfix']//input[@type='submit' and @value='Send Now']";

    
    private final WebDriverWrapper driver;

    public InvitePageForm(WebDriverWrapper driver) {
	this.driver = driver;
    }

    // ************************ Actions **************************

    public void clickCopyLinkBtn() {
	driver.waitAndClick("COPY_LINK_BTN", By.xpath(COPY_LINK_BTN));
    }

    public FacebookLoginPage clickShareOnFacebookBtn() {

	FacebookLoginPage loginPage = new FacebookLoginPage(driver);

	WebElement facebookLink = driver.waitAndGetElement("SHARE_ON_FACEBOOK", By.xpath(SHARE_ON_FACEBOOK));
	String url = facebookLink.getAttribute(HTML.HREF);
	driver.get(url);

	if (loginPage.isPageLoaded()) {
	    return loginPage;
	} else {
	    return null;
	}

    }

    public TwitterSignInPage clickShareOnTwitterBtn() {

	driver.saveBeforeHandles();
	driver.waitAndClick("SHARE_ON_TWITTER", By.xpath(SHARE_ON_TWITTER));

	TwitterSignInPage signInPage = new TwitterSignInPage(driver);

	if (signInPage.isPageLoaded()) {
	    return signInPage;
	} else {
	    return null;
	}
    }

    public void sendInvitations(List<String> toEmails) {

	String emailsText = "";
	for (String toEmail : toEmails) {
	    emailsText += toEmail + ", ";
	}
	emailsText = StringUtils.removeEnd(emailsText, ", ");

	driver.waitAndSendKeys("EMAILS_TEXTAREA", By.xpath(EMAILS_TEXTAREA), emailsText);
	driver.waitAndClick("SEND_NOW_BTN", By.xpath(SEND_NOW_BTN));
    }

    public PlaxoContactPage clickAddFromYourContactsBtn() {

	driver.saveBeforeHandles();
	driver.waitAndClick("ADD_FROM_CONTACTS_BTN", By.xpath(ADD_FROM_CONTACTS_BTN));

	PlaxoContactPage contactPage = new PlaxoContactPage(driver);

	if (contactPage.isPageLoaded()) {
	    return contactPage;
	} else {
	    return null;
	}
    }

    // ************************ Validations **************************
    
    public boolean isInviteFormPresent() {
	return driver.waitAndVerifyElementPresent(By.xpath(INVITE_FORM));
    }

    public boolean isBaseElementsPresent() {
	driver.waitAndCheckElementPresent("PERSONAL_LINK", By.xpath(PERSONAL_LINK));
	driver.waitAndCheckElementPresent("COPY_LINK_BTN", By.xpath(COPY_LINK_BTN));

	driver.waitAndCheckElementPresent("SHARE_ON_FACEBOOK", By.xpath(SHARE_ON_FACEBOOK));
	driver.waitAndCheckElementPresent("SHARE_ON_TWITTER", By.xpath(SHARE_ON_TWITTER));

	driver.waitAndCheckElementPresent("EMAILS_TEXTAREA", By.xpath(EMAILS_TEXTAREA));
	driver.waitAndCheckElementPresent("ADD_FROM_CONTACTS_BTN", By.xpath(ADD_FROM_CONTACTS_BTN));
	driver.waitAndCheckElementPresent("SEND_NOW_BTN", By.xpath(SEND_NOW_BTN));

	return true;
    }

    public boolean isCopyLinkBtnTextChanged() {
	driver.waitAndCheckElementNotPresent("COPY_LINK_BTN", By.xpath(COPY_LINK_BTN));
	driver.waitAndCheckElementPresent("COPIED_BTN", By.xpath(COPIED_BTN));
	return true;
    }
}
